
/*

NOTES

Replication do-file for publication in World Development for 
		Resource rents, coercion, and local development: 
			Evidence from post-apartheid South Africa

					Paulo Bastos 	(World Bank)
					Nicolas Bottan 	(Cornell University)

This do-file generates Figures 2 to 10.
					
*/


* Set WD
cd ""


* Set Output Dir
global results "results/figures/"



use data/WD_panel_census2011_centroid.dta, clear


gen t=year==2011
xtset spcode t

gen cou=1

replace water_piped_pipedex_public = water_piped+water_pipedex+water_public if year==1996
gen pipedwater= water_piped_pipedex_public

* Initial population as weight
cap: drop aux
gen weight=totpop if year==1996
egen aux=max(weight), by(spcode)
replace weight=aux
drop aux

gen ldpop=(log(f.totpop)-log(totpop))
gen ldinc=(log(f.pcincome)-log(pcincome))


matrix input LAT1=(-33.926, -29.858, -26.202, -26.268, -25.745, -33.918, -29.617, -26.188, -25.996, -33.015)
matrix input LON1=(18.423, 31.029, 28.044, 27.858, 28.188, 25.57, 30.393, 28.321, 28.227, 27.912)
matrix input POP=(3433441, 3120282, 2026469, 1695047, 1619438, 967677, 750845, 605344, 511655, 478676)

forvalues i=1(1)10 {
	local a=LAT1[1,`i']
	local b=LON1[1,`i']
	vincenty Y X `a' `b' , l(city`i') inkm
	}
egen city_min=rowmin(city1-city10)
egen city_median=rowmedian(city1-city10)
egen city_average=rowmean(city1-city10)
egen city_p30=rowpctile(city1-city10), p(30)
gen city_waverage=0.23*city1 + 0.21*city2 + 0.13*city3 + 0.11*city4 + 0.11*city5 + 0.06*city6 + 0.05*city7 + 0.04*city8 + 0.03*city9 + 0.03*city10


keep if dist<30


***
* COLLAPSE BY DISTANCE BIN

preserve
collapse (mean) distroad city_waverage pipedwater  pcincome* ldinc* employed* race_* dwel_* mage* educ_* light_* phone_* internet_* water* moved_last10yrs refuse* home* hedu* [w=weight], by(group year)

sort group
tempfile gr
save "`gr'", replace
restore
collapse (mean) ldpop totpop (sum) cou, by(group)
sort group
merge group using "`gr'"
drop _mer


global cifit lcolor(black) fcolor(gs12) alcolor(gs12)
global dopt mcolor(black) m(O)
global twopt xline(0) legend(off) scheme(s1mono) xtitle(Distance to border in km) xscale(range(-30 30)) xtick(-30 -20 -10 0 10 20 30) xlab(-30 -20 -10 0 10 20 30)

















*** % Black 1996 and 2001
local depvar96 race_black
local depvar race_black
local yt "Share Black"


local yrange "yscale(range(.4 1)) ytick(0.4 0.5 0.6 0.7 0.8 0.9 1) ylab(0.4 0.5 0.6 0.7 0.8 0.9 1)"

* 1996
twoway (lfitci `depvar96' group if group<0 & year==1996, $cifit) (lfitci `depvar96' group if group>0 & year==1996, $cifit) (scatter `depvar96' group if year==1996, $dopt), $twopt ytitle("`yt'")  `yrange' title("(a) 1996")
graph export ${results}Fig2a.pdf, replace
* 2011
twoway (lfitci `depvar' group if group<0 & year==2011, $cifit) (lfitci `depvar' group if group>0 & year==2011, $cifit) (scatter `depvar' group if year==2011, $dopt), $twopt ytitle("`yt'") `yrange' title("(b) 2011")
graph export ${results}Fig2b.pdf, replace




* Per capita income
replace pcincome=pcincome/1000
local yt "Per capita income (in 1000's of rand)"

local depvar96 pcincome
local depvar pcincome

local yrange "yscale(range(0 100)) ytick(0 20 40 60 80 100) ylab(0 20 40 60 80 100)"


* 1996
twoway (lfitci `depvar96' group if group<0 & year==1996, $cifit) (lfitci `depvar96' group if group>0 & year==1996, $cifit) (scatter `depvar96' group if year==1996, $dopt), $twopt ytitle("`yt'") title("(a) 1996") `yrange'
graph export ${results}Fig3a.pdf, replace
* 2011
twoway (lfitci `depvar' group if group<0 & year==2011, $cifit) (lfitci `depvar' group if group>0 & year==2011, $cifit) (scatter `depvar' group if year==2011, $dopt), $twopt ytitle("`yt'") title("(b) 2011") `yrange'
graph export ${results}Fig3b.pdf, replace







* difference in log pc income
local depvar ldinc
local yt "Growth in per capita income"

* 2011
twoway (lfitci `depvar' group if group<0 & year==1996, $cifit) (lfitci `depvar' group if group>0 & year==1996, $cifit) (scatter `depvar' group if year==1996, $dopt), $twopt ytitle("`yt'") 
graph export ${results}Fig4.pdf, replace





* Employed in mining
local depvar96 employed_mining
local yt "Share employed in mining"

local yrange "yscale(range(-0.04 .24)) ytick(-0.04 0 0.04 0.08 0.12 0.16 0.20 0.24) ylab(-0.04 0 0.04 0.08 0.12 0.16 0.20 0.24)"

* 1996
twoway (lfitci `depvar96' group if group<0 & year==1996, $cifit) (lfitci `depvar96' group if group>0 & year==1996, $cifit) (scatter `depvar96' group if year==1996, $dopt), $twopt ytitle("`yt'") 
graph export ${results}Fig5.pdf, replace









*** INFRASTRUCTURE, MARKET ACCESS AND CONNECTIVITY

* Electricity
local depvar96 light_elgrid
local depvar light_elgrid
local yt "Share Connected to electric grid"

local yrange "yscale(range(0 1)) ytick(0 .2 .4 .6 .8 1) ylab(0 .2 .4 .6 .8 1)"

* 1996
twoway (lfitci `depvar96' group if group<0 & year==1996, $cifit) (lfitci `depvar96' group if group>0 & year==1996, $cifit) (scatter `depvar96' group if year==1996, $dopt), $twopt ytitle("`yt'") title("(a) 1996") `yrange'
graph export ${results}Fig8Aa.pdf, replace
* 2011
twoway (lfitci `depvar' group if group<0 & year==2011, $cifit) (lfitci `depvar' group if group>0 & year==2011, $cifit) (scatter `depvar' group if year==2011, $dopt), $twopt ytitle("`yt'") title("(b) 2011") `yrange'
graph export ${results}Fig8Ab.pdf, replace



* No refuse
local depvar96 refuse_none
local depvar refuse_none
local yt "Share No refuse"

local yrange "yscale(range(0 0.40)) ytick(0 .1 .2 .3 .4) ylab(0 .1 .2 .3 .4)"

* 1996
twoway (lfitci `depvar96' group if group<0 & year==1996, $cifit) (lfitci `depvar96' group if group>0 & year==1996, $cifit) (scatter `depvar96' group if year==1996, $dopt), $twopt ytitle("`yt'") title("(a) 1996") `yrange'
graph export ${results}Fig8Ba.pdf, replace
* 2011
twoway (lfitci `depvar' group if group<0 & year==2011, $cifit) (lfitci `depvar' group if group>0 & year==2011, $cifit) (scatter `depvar' group if year==2011, $dopt), $twopt ytitle("`yt'") title("(b) 2011") `yrange'
graph export ${results}Fig8Bb.pdf, replace



* Piped water
local depvar96 pipedwater
local depvar pipedwater
local yt "Share Piped water (inside or outside home)"

local yrange "yscale(range(0 1)) ytick(0 .2 .4 .6 .8 1) ylab(0 .2 .4 .6 .8 1)"

* 1996
twoway (lfitci `depvar96' group if group<0 & year==1996, $cifit) (lfitci `depvar96' group if group>0 & year==1996, $cifit) (scatter `depvar96' group if year==1996, $dopt), $twopt ytitle("`yt'") title("(a) 1996") `yrange'
graph export ${results}Fig8Ca.pdf, replace
* 2011
twoway (lfitci `depvar' group if group<0 & year==2011, $cifit) (lfitci `depvar' group if group>0 & year==2011, $cifit) (scatter `depvar' group if year==2011, $dopt), $twopt ytitle("`yt'") title("(b) 2011") `yrange'
graph export ${results}Fig8Cb.pdf, replace





* Distance to roads
local depvar96 distroad
local yt "Proximity to main road"

twoway (lfitci `depvar96' group if group<0 & year==1996, $cifit) (lfitci `depvar96' group if group>0 & year==1996, $cifit) (scatter `depvar96' group if year==1996, $dopt), $twopt ytitle("`yt'") 
graph export ${results}Fig9B.pdf, replace




* Distance to main cities
local depvar96 city_waverage
local yt "Distance to main cities"

twoway (lfitci `depvar96' group if group<0 & year==1996, $cifit) (lfitci `depvar96' group if group>0 & year==1996, $cifit) (scatter `depvar96' group if year==1996, $dopt), $twopt ytitle("`yt'")  
graph export ${results}Fig9A.pdf, replace









****************************************
*** By High/low employment in mining ***
****************************************

use data/WD_panel_census2011_centroid.dta, clear


gen t=year==2011
xtset spcode t

* Initial population as weight
cap: drop aux
gen weight=totpop if year==1996
egen aux=max(weight), by(spcode)
replace weight=aux
drop aux

gen cou=1

xtset spcode t

gen ldinc=log(f.pcincome)-log(pcincome)

replace edu_second=hedu_second if edu_second==.
replace edu_higher=hedu_higher if edu_higher==.
gen highed=edu_second+edu_higher
gen dedu=f.highed-highed

sum employed_mining, det
gen high=employed_mining>r(p90)

keep if year==1996

preserve
collapse (mean) pcincome* dedu highed hedu*  ldinc employed* race_* dwel_* mage* educ_* light_* phone_* refuse* home* [w=weight], by(group high)

sort group high
tempfile gr
save "`gr'", replace
restore
collapse (mean) totpop* (sum) cou, by(group high)
sort group high
merge group high using "`gr'"
drop _mer

global twopt xline(0) scheme(s1mono) xtitle(Distance to border in km) xscale(range(-30 30)) xtick(-30 -20 -10 0 10 20 30) xlab(-30 -20 -10 0 10 20 30)

global dopt2 mcolor(red) m(X)

local depvar ldinc
local yt "Growth in per capita income"

sum ldinc if high
sum ldinc if high==0

twoway (scatter `depvar' group if high==1, $dopt) (scatter `depvar' group if high==0, $dopt2) (lfit `depvar' group if high==1 & group<0, lcolor(black)) (lfit `depvar' group if high==0 & group<0, lcolor(red) lpattern(dash)) (lfit `depvar' group if high==1 & group>0, lcolor(black)) (lfit `depvar' group if high==0 & group>0, lcolor(red)lpattern(dash)) , $twopt ytitle("`yt'") yscale(range( ))  legend(order(1 "High share mining" 2 "Other") position(7) ring(0) cols(1) region(lcolor(none) fcolor(none)))
graph export ${results}Fig6.pdf, replace



local depvar hedu_none
local yt "% No formal education"

sum hedu_none if high
sum hedu_none if high==0

twoway (scatter `depvar' group if high==1, $dopt) (scatter `depvar' group if high==0, $dopt2) (lfit `depvar' group if high==1 & group<0, lcolor(black)) (lfit `depvar' group if high==0 & group<0, lcolor(red) lpattern(dash)) (lfit `depvar' group if high==1 & group>0, lcolor(black)) (lfit `depvar' group if high==0 & group>0, lcolor(red)lpattern(dash)) , $twopt ytitle("`yt'") yscale(range( ))  legend(order(1 "High share mining" 2 "Other") position(7) ring(0) cols(1) region(lcolor(none) fcolor(none)))
graph export ${results}Fig7A.pdf, replace


local depvar highed
local yt "% Completed HS or More"

sum highed if high
sum highed if high==0

twoway (scatter `depvar' group if high==1, $dopt) (scatter `depvar' group if high==0, $dopt2) (lfit `depvar' group if high==1 & group<0, lcolor(black)) (lfit `depvar' group if high==0 & group<0, lcolor(red) lpattern(dash)) (lfit `depvar' group if high==1 & group>0, lcolor(black)) (lfit `depvar' group if high==0 & group>0, lcolor(red)lpattern(dash)) , $twopt ytitle("`yt'") yscale(range( ))  legend(order(1 "High share mining" 2 "Low share mining") position(11) ring(0) cols(1) region(lcolor(none) fcolor(none)))
graph export ${results}Fig7B.pdf, replace







* Figure 10

use data/db_for_multiplier, clear

gen svy=((svycoeff)*15)*(0.0423298)
gen cml=coeff*0.0423298
gen multiplier=cml/svy

sum multiplier, det

twoway (hist multiplier, w(.12)) (scatteri 0 1.552658 1.1 1.552658, recast(line) lcolor(red))  (scatteri 0 1.196785 1.1 1.196785, recast(line) lcolor(red) lpattern(dash))  (scatteri 0 1.842576 1.1 1.842576, recast(line)  lcolor(red) lpattern(dash)),  scheme(s1mono) legend(off) note("Median (solid line); 25-75 percentile (dashed line)") ytitle("Density") xtitle("Multiplier Estimate")

graph export ${results}Fig10.pdf, replace
